package com.ld.security.auth.common.sys.service.impl;


import com.ld.security.auth.common.sys.constant.UserCacheConstants;
import com.ld.security.auth.common.sys.entity.SysUserEntity;
import com.ld.security.auth.common.sys.mapping.SysUserMapping;
import com.ld.security.auth.common.sys.po.UpdatePasswordPo;
import com.ld.security.auth.common.sys.service.ISysUserService;
import com.ld.security.auth.common.sys.vo.UserDetailsVo;
import com.ld.security.auth.common.sys.vo.UserInfoVo;
import com.ld.security.auth.common.sys.vo.UserMapVo;
import com.ld.security.common.core.exception.ApiException;
import com.ld.security.common.core.util.CrptUtil;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.core.util.UuidUtil;
import com.ld.security.common.mybatis.base.service.ISuperServiceImpl;
import com.ld.security.common.mybatis.util.JdbcUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SysUserServiceImpl  extends ISuperServiceImpl<SysUserEntity>
implements
        ISysUserService {

        @Autowired
        SysUserMapping sysUserMapping;


  /*      private void flushByUsername(String id){
                SysUserEntity sysUserEntity =  this.getById(id);
                if(sysUserEntity != null){
                        flushByUsername(sysUserEntity);
                }
        }
        private void flushByUsername(  SysUserEntity sysUserEntity){
                        String username = sysUserEntity.getUsername();
                        EventUtil.dispatch(UserService.class,"flushByUsername",username);
        }*/

        @CacheEvict(cacheNames = {UserCacheConstants.userInfo,UserCacheConstants.userDetails},key = "#entity.username")
        @Override
        public int save(SysUserEntity entity) {
                boolean hash=false;
                if(StringUtil.isNotNull(entity.getId())){
                        SysUserEntity usser =  this.getById(entity.getId());
                        if(entity.getPassword().equals(usser.getPassword())){

                        }else{
                                hash = true;

                        }
                }else{
                        hash = true;
                }
                if(hash){
                        String newPass = UuidUtil.md5(entity.getPassword());
                        entity.setPassword(newPass);
                }

                return super.save(entity);
        }

        @CacheEvict(cacheNames = {UserCacheConstants.userInfo,UserCacheConstants.userDetails},key = "#po.username")
        @Override
        public int updatePassword(UpdatePasswordPo po) {
                CrptUtil.encrpt(po);
                SysUserEntity userEntity =  this.newInstance();
                userEntity.setId(po.getId());
                userEntity.setPassword(po.getPassword());
                return sysUserMapping.updateById(userEntity);
        }


        @Override
        public void register(SysUserEntity user) {
                if(StringUtil.isNull(user.getId())){
                        ApiException.throw400("id必须为空");
                }
                this.save(user);
        }

        @Override
        public UserMapVo findUserMap(List<String> ids) {
                UserMapVo vo = new UserMapVo(ids);
                JdbcUtil.proxyQuery(vo);
                return vo;
        }



        @Override
        @Cacheable(cacheNames = UserCacheConstants.userInfo,key = "#username")
        public UserInfoVo getUserInfo(String username) {
                UserInfoVo info = new UserInfoVo();
                info.setUsername(username);
                this.proxyQuery(info);
                return info;
        }
        @Override
        @Cacheable(cacheNames = UserCacheConstants.userDetails,key = "#username")
        public UserDetailsVo getUserDetails(String username) {
                UserDetailsVo userDetailsVo =  new UserDetailsVo();
                userDetailsVo.setUsername(username);
                this.proxyQuery(userDetailsVo);
                return userDetailsVo;
        }

        @Override
        @CacheEvict(cacheNames = {UserCacheConstants.userInfo,UserCacheConstants.userDetails})
        public void flushByUsername(String username){

        }
        @Override
        @CacheEvict(cacheNames =  {UserCacheConstants.userInfo,UserCacheConstants.userDetails},allEntries = true)
        public void flushAll(){

        }



}
